package com.yangcangclub.api.v1.message.controller;

import com.yangcangclub.commons.annotation.ApolloSecurity;
import com.yangcangclub.commons.annotation.TimeEnum;
import com.yangcangclub.commons.exception.ApolloException;
import com.yangcangclub.commons.result.MessageEnum;
import com.yangcangclub.commons.result.ResultData;
import com.yangcangclub.commons.utils.StringUtil;
import com.yangcangclub.message.service.MessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 消息接口
 */
@Controller
@RequestMapping("/yangcangclub/v1/message")
public class MessageController {
    private static Logger logger = LoggerFactory.getLogger(MessageController.class);


    @Autowired
    MessageService messageService;


    /**
     * 发送手机短信
     */
    @ResponseBody
    @RequestMapping(value = "/sms",method= RequestMethod.GET)
    @ApolloSecurity(limit = 5,time = TimeEnum.MINUTE)
//    @RequestSign
    public ResultData sms (String phone,String type,
                                @RequestParam(value ="picCode", defaultValue = "") String picCode
    ) {
        ResultData  resultData ;
        try {
            if(phone==null||type==null){
                throw new ApolloException(MessageEnum.PARMA_IS_EMPTY);
            }else if(StringUtil.isNotNumber(type)||Integer.valueOf(type)<0||Integer.valueOf(type)>2){
                throw new ApolloException(MessageEnum.PARMA_FORMAT_ERROR);
            }

//            else if(picCode==null||picCode==null){
//                throw new ApolloException(MessageEnum.PARMA_IS_EMPTY);
//            }
//            phone ="+"+phone;
            boolean b= messageService.sendSMS(phone,Integer.valueOf(type),picCode,"");
//            boolean b = true;
            if(b){
                resultData=new ResultData(MessageEnum.SUCCESS);
            }else{
                resultData=new ResultData(MessageEnum.FAILURE);
            }

        } catch (ApolloException apoEx) {
            resultData=new ResultData(apoEx);
        }catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultData=new ResultData(e);
        }

        return resultData;
    }

}
